package com.bindapp.model;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.bindapp.R;
import com.bindapp.activity.DevicePlayActivity;
import com.bindapp.adapter.JKGLAdapter;
import com.bindapp.databinding.ActivityJkglBinding;
import com.bindapp.listener.RecyclerItemClick;
import com.bindapp.util.ezutil.EZUtils;
import com.bindapp.widget.GridDecoration;
import com.videogo.constant.IntentConsts;
import com.videogo.openapi.bean.EZCameraInfo;
import com.videogo.openapi.bean.EZDeviceInfo;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static com.bindapp.App.getOpenSDK;

/**
 * Created by apple on 2018/7/5.
 */

public class JKGLModel extends BaseModel {
    private ActivityJkglBinding binding;
    private JKGLAdapter adapter;
    private List<Object> datalist= new ArrayList<Object>();
    //private List<EZDeviceInfo> mlist=new ArrayList<EZDeviceInfo>();

    public JKGLModel(Context context){
        super(context);
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.activity_jkgl,null,false);
        //textdata();

        initView();
        textData();

    }

    public void textdata(){
        for(int i=0;i<5;i++){
            datalist.add(""+i);
        }
    }

    public void initView(){
        adapter=new JKGLAdapter(datalist,R.layout.item_jkgl,-1,new ItemClick());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup(){

            @Override
            public int getSpanSize(int position) {
                return 1;
            }
        });
        binding.recycleview.setLayoutManager(gridLayoutManager);
        binding.recycleview.addItemDecoration(new GridDecoration(context,0,3,false));
        binding.recycleview.setAdapter(adapter);
    }

    class ItemClick implements RecyclerItemClick{

        @Override
        public void onItemClick(int position) {
            /*EZDeviceInfo deviceInfo=(EZDeviceInfo)datalist.get(0);
            EZCameraInfo cameraInfo = EZUtils.getCameraInfoFromDevice(deviceInfo, 0);
            Intent intent=new Intent(context,DevicePlayActivity.class);
            intent.putExtra(IntentConsts.EXTRA_DEVICE_INFO, deviceInfo);
            intent.putExtra(IntentConsts.EXTRA_CAMERA_INFO, cameraInfo);
            context.startActivity(intent);*/

        }

        @Override
        public void onItemLongClick(int position) {

        }

        @Override
        public void onMoreClick(View view, int position) {
            Intent intent=null;
            switch (view.getId()){
                case R.id.setimg:
                    EZDeviceInfo deviceInfo=(EZDeviceInfo)datalist.get(0);
                    EZCameraInfo cameraInfo = EZUtils.getCameraInfoFromDevice(deviceInfo, 0);
                    intent=new Intent(context,DevicePlayActivity.class);
                    intent.putExtra(IntentConsts.EXTRA_DEVICE_INFO, deviceInfo);
                    intent.putExtra(IntentConsts.EXTRA_CAMERA_INFO, cameraInfo);
                    intent.putExtra("title",position+"号摄像头");
                    intent.putExtra("isSet",true);
                    context.startActivity(intent);
                    break;
                case R.id.setview:
                    EZDeviceInfo deviceInfo2=(EZDeviceInfo)datalist.get(0);
                    EZCameraInfo cameraInfo2 = EZUtils.getCameraInfoFromDevice(deviceInfo2, 0);
                    intent=new Intent(context,DevicePlayActivity.class);
                    intent.putExtra(IntentConsts.EXTRA_DEVICE_INFO, deviceInfo2);
                    intent.putExtra(IntentConsts.EXTRA_CAMERA_INFO, cameraInfo2);
                    intent.putExtra("title",position+"号摄像头");
                    intent.putExtra("isSet",true);
                    context.startActivity(intent);
                    break;
            }

        }
    }

    @Override
    public ActivityJkglBinding getBinding() {
        return binding;
    }


    private void textData(){
        Observable.fromArray(1)
                .subscribeOn(Schedulers.io())
                .map(new Function<Integer, Object>() {

                    @Override
                    public Object apply(@NonNull Integer integer) throws Exception {
                        datalist.clear();
                        for(int i=0;i<6;i++){
                            datalist.addAll(getOpenSDK().getDeviceList(0, 20));
                        }

                        return "";
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onNext(Object o) {

                    }

                    @Override
                    public void onComplete() {
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                });


    }


}
